package com.zHan.controller;

import com.zHan.dto.QueryPageDto;
import com.zHan.pojo.Article;
import com.zHan.pojo.PageBean;
import com.zHan.pojo.Result;
import com.zHan.service.ArticleService;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: ArticleController
 * Description:
 * date: 2023/11/8 0008 0:32
 *
 * @author zHan
 */
@RestController
@RequestMapping("/article")
@Validated
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     *
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class) Article article) {
        return articleService.add(article);
    }

    /**
     * 文章列表(条件分页)
     *
     * @param dto
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> queryPage(@Validated QueryPageDto dto,
                                               @RequestParam(required = false) Integer categoryId,
                                               @RequestParam(required = false) String state) {
        return articleService.queryPage(dto, categoryId, state);
    }

    /**
     * 获取文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result detail(@NotNull Integer id) {
        return articleService.findById(id);
    }

    /**
     * 更新文章
     *
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article) {
        return articleService.update(article);
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(@NotNull Integer id) {
        return articleService.delete(id);
    }
}
